<?php
/**
 * 分页类
 * @author gray.guo
 */
namespace bc\util\backend;
use yii;


class Page
{
	private $total; // 总记录
	private $pagesize; // 每页显示多少条
	private $page; // 当前页码
	private $pagenum; // 总页码
	private $url; // 地址
	private $showTotal;
	private $bothnum = 5; // 两边保持数字分页的量
	                  
	// 构造方法初始化
	public function __construct( $_total, $_pagesize, $ajax = false)
	{
		$this->total = $_total ? $_total : 1;
		$this->showTotal = $_total;
		$this->pagesize = $_pagesize;
		$this->pagenum = ceil ( $this->total / $this->pagesize );
		$this->page = $this->setPage ();
		
		if($ajax === false)
			$this->url = $this->setUrl () ;
		else 
			$this->url = 'javascript::void(0)';
	}
	
	/**
	 * @return integer
	 */
	public function getOffset()
	{
		$page = Yii::$app->request->get('page', 1);
		return ($page - 1) * $this->pagesize;
	}
	
	// 获取当前页码
	private function setPage()
	{
		$page = Yii::$app->request->get('page', 0);
		if (! empty ( $page ))
		{
			if ($page > 0)
			{
				if ($page > $this->pagenum)
				{
					return $this->pagenum;
				}
				else
				{
					return $page;
				}
			}
			else
			{
				return 1;
			}
		}
		else
		{
			return 1;
		}
	}
	
	// 获取地址
	private function setUrl()
	{
		$_url = $_SERVER ["REQUEST_URI"];
		$_par = parse_url ( $_url );
		if (isset ( $_par ['query'] ))
		{
			parse_str ( $_par ['query'], $_query );
			unset ( $_query ['page'] );
			$_url = $_par ['path'] . '?' . http_build_query ( $_query );
		}
		else 
		{
			$_url = $_par['path'] . '?';
		}
		
		return $_url;
	} 
	
	
	// 数字目录
	private function pageList()
	{
		$_pagelist  = '';
		for($i = $this->bothnum; $i >= 1; $i --)
		{
			$_page = $this->page - $i;
			if ($_page < 1)
				continue;
			$_pagelist .= ' <li><a href="' . $this->url . '&page=' . $_page . '"  page="' . $_page  . '">' . $_page . '</a></li> ';
		}
		$_pagelist .= ' <li class="active">' . $this->page . '</li> ';
		for($i = 1; $i <= $this->bothnum; $i ++)
		{
			$_page = $this->page + $i;
			if ($_page > $this->pagenum)
				break;
			$_pagelist .= ' <li><a href="' . $this->url . '&page=' . $_page . '" page=" '  .  $_page . '">' . $_page . '</a></li> ';
		}
		return $_pagelist;
	}
	
	// 首页
	private function first()
	{
		if ($this->page > $this->bothnum + 1)
		{
			return ' <li><a href="' . $this->url . '" data-page="1">1</a> ...</li>';
		}
	}
	
	// 上一页
	private function prev()
	{
		if ($this->page == 1)
		{
			return '<li>上一页</li>';
		}
		return ' <li><a href="' . $this->url . '&page=' . ($this->page - 1) . '" data-page="' . ($this->page - 1) . '">上一页</a></li> ';
	}
 	
	// 下一页
	private function next()
	{
		if ($this->page == $this->pagenum)
		{
			return '<li>下一页</li>';
		}
		return ' <li><a href="' . $this->url . '&page=' . ($this->page + 1) . '" data-page="' . ($this->page +1 ) . '">下一页</a></li> ';
	}
	
	// 尾页
	private function last()
	{
		if ($this->pagenum - $this->page > $this->bothnum)
		{
			return ' <li>...<a href="' . $this->url . '&page=' . $this->pagenum . '" data-page="' . $this->pagenum . '">' . $this->pagenum . '</a></li> ';
		}
	}
	
	protected function totalPage()
	{
		return $span = "<span>共{$this->showTotal}条信息</span>";
	}
	
	
	// 分页信息
	public function __toString()
	{
		$_page = $this->totalPage();
		$_page .= '<ul>';
		$_page .= $this->prev ();
		$_page .= $this->first ();
		$_page .= $this->pageList ();
		$_page .= $this->last ();
		$_page .= $this->next ();
		$_page .= '</ul>';
		return $_page;
	}
}
